/*
 * MP3.c
 *
 * Created: 2012-07-09 13:00:11
 *  Author: Sebastian_2
 */ 

#include "Menus.h"

void Menu_MP3_Enter() {
	gMenu = MENU_MP3;
	point p = {72, 48};
	point p2 = {245, 122};
	gColour = SUBMENU_BACKGROUND;
	SSD1289_DrawRect(&p, &p2);
	drawFrame(&p, &p2, SEPARATOR);
	gColour = FONT_COLOUR;
	p = (point){77, 54};
	SSD1289_PrintString("Odtwarzacz", &p, &droidSans22ptFontInfo);
	p = (point){72, 84};
	drawXSeparator(&p, 245, SEPARATOR);
	p = (point){77, 90};
	SSD1289_PrintString("Wykonawcy", &p, &droidSans22ptFontInfo);
}

void Menu_MP3_BackToMain() {
	gMenu = MENU_MAIN;
	point p = {72, 48};
	point p2 = {246, 123};
	gColour = BACKGROUND;
	SSD1289_DrawRect(&p, &p2);
	p = (point){94, 56};
	ShowRGB565("/ico/radio.565", &p);
	p.x = 169;
	ShowRGB565("/ico/calendar.565", &p);
}

void Menu_MP3_Player_Enter() {
	if(gStatus & PLAYER_SSELECTED) {
		Menu_MP3_Player_Refresh();
	} else {
		SSD1289_ClearOmitBar();
		point p = {20, 40};
		SSD1289_PrintString("Nie wybrano utworu", &p, &droidSans22ptFontInfo);
		_delay_ms(200);
		Menu_Main_Enter();
	}
}

void Menu_MP3_Player_Refresh() {
	FIL file;
	FRESULT res;
	char tmp[100];
	point p;
	uint8_t pos;
	char *ptr;
	
	sprintf(tmp, "/music/%s/CNT", mArtistsDirList[mArtistsCurrentDir]);
	res = f_open(&file, tmp, FA_READ | FA_OPEN_EXISTING);
	f_sync(&file);
	f_gets(tmp, sizeof(tmp), &file);
	f_sync(&file);
	f_close(&file);
	sscanf(tmp, "%d", (int*)&mSongsMaxItems);
	pos = mSongsCurrentPage*ITEMS_ON_PAGE + mSongsSelectedRow;
	if(pos > mSongsMaxItems-1)
		return;
	if(gMenu != MENU_MP3_PLAYER) {
		SSD1289_ClearOmitBar();
		p = (point){7, 169};
		ShowRGB565("/ico/prev.565", &p);
		p = IncX(&p, 63);
		ShowRGB565("/ico/play.565", &p);
		p = IncX(&p, 63);
		ShowRGB565("/ico/stop.565", &p);
		p = IncX(&p, 63);
		ShowRGB565("/ico/repeat.565", &p);
		p = IncX(&p, 63);
		ShowRGB565("/ico/next.565", &p);
	} else {
		p = (point){0, 20};
		point p2 = {SSD1289_MaxX, 169};
		gColour = BACKGROUND;
		SSD1289_DrawRect(&p, &p2);
	}
	
	gMenu = MENU_MP3_PLAYER;
	
	sprintf(tmp, "/music/%s/IDX", mArtistsDirList[mArtistsCurrentDir]);
	res = f_open(&file, tmp, FA_READ | FA_OPEN_EXISTING);
	f_sync(&file);
	CheckRes();
	
	for(uint8_t i = 0; i < pos; i++) {
		f_gets(tmp, sizeof(tmp), &file);
		f_sync(&file);
	}
	if(f_gets(tmp, sizeof(tmp), &file) == tmp) {
		f_sync(&file);
		char separator = 0x03;
		uint16_t len;
		
		ptr = strtok(tmp, &separator);
		len = TextLength(ptr, &droidSans22ptFontInfo);
		p = (point){(SSD1289_MaxX-len)/2, 45};
		gColour = FONT_COLOUR;
		SSD1289_PrintString(ptr, &p, &droidSans22ptFontInfo);
		
		ptr = strtok(NULL, &separator);
		len = TextLength(ptr, &droidSans22ptFontInfo);
		p = (point){(SSD1289_MaxX-len)/2, 85};
		SSD1289_PrintString(ptr, &p, &droidSans22ptFontInfo);
		gStatus |= PLAYER_SSELECTED;
	}
}

void Menu_MP3_Player_Previous() {
	if(mSongsSelectedRow >= 1) {
		mSongsSelectedRow--;
		Menu_MP3_Player_Refresh();
	} else if(mSongsSelectedRow == 0 && mSongsCurrentPage >= 1) {
		mSongsCurrentPage--;
		mSongsSelectedRow = ITEMS_ON_PAGE-1;
		Menu_MP3_Player_Refresh();
	}
}

void Menu_MP3_Player_Next() {
	uint8_t pos = mSongsCurrentPage*ITEMS_ON_PAGE + mSongsSelectedRow;
	if(pos >= mSongsMaxItems-1)
		return;
	if(mSongsSelectedRow < ITEMS_ON_PAGE-1) {
		mSongsSelectedRow++;
		Menu_MP3_Player_Refresh();
	} else {
		mSongsSelectedRow = 0;
		mSongsCurrentPage++;
		Menu_MP3_Player_Refresh();
	}
}

void Menu_MP3_Player_PlayPause() {
	point p = {70, 169};
	
	if(gStatus & PLAYER_STOP) {
		gStatus &= ~PLAYER_STOP;
		gStatus |= PLAYER_PLAY;
		ShowRGB565("/ico/pause.565", &p);
	} else if(gStatus & PLAYER_PLAY) {
		gStatus &= ~PLAYER_PLAY;
		gStatus |= PLAYER_PAUSE;
		point p = {70, 169};
		ShowRGB565("/ico/pause.565", &p);
	} else if(gStatus & PLAYER_PAUSE) {
		gStatus &= ~PLAYER_PAUSE;
		gStatus |= PLAYER_PLAY;
		point p = {70, 169};
		ShowRGB565("/ico/play.565", &p);
	}
}

void Menu_MP3_Player_Stop() {
	point p = {70, 169};
	if(gStatus & PLAYER_PLAY) {
		gStatus &= ~PLAYER_PLAY;
		ShowRGB565("/ico/play.565", &p);
	} else if(gStatus & PLAYER_PAUSE) {
		gStatus &= ~PLAYER_PAUSE;
	}
	gStatus |= PLAYER_STOP;
	gStatus &= ~PLAYER_SSELECTED;
}

void Menu_MP3_Player_RepeatToggle() {
	point p = {196, 169};
	if(gStatus & PLAYER_REPEAT) {
		gStatus &= ~PLAYER_REPEAT;
		ShowRGB565("/ico/repeat.565", &p);
	} else {
		gStatus |= PLAYER_REPEAT;
		ShowRGB565("/ico/repeat2.565", &p);
	}
}

void Menu_MP3_Artists_Enter() {
	mArtistsCurrentPage = 0;
	Menu_MP3_Artists_Refresh();
}

void Menu_MP3_Artists_Refresh() {
	gMenu = MENU_MP3_ARTISTS;
	SSD1289_ClearOmitBar();
	
	FIL file;
	char tmp[50];
	char *ptr;
	point p = {10, 28};

	f_open(&file, "/music/IDX", FA_READ | FA_OPEN_EXISTING);
	f_sync(&file);
	// first go to wanted entry
	for(uint8_t i = 0; i < mArtistsCurrentPage*ITEMS_ON_PAGE; i++) {
		f_gets(tmp, sizeof(tmp), &file);
		f_sync(&file);
	}
	// now scan it and write it to global variable
	uint8_t counter = 0;
	for(uint8_t i = 0; i < ITEMS_ON_PAGE; i++) {
		if(f_gets(tmp, sizeof(tmp), &file) == tmp) {
			f_sync(&file);
			char separator = 0x03;
			ptr = strtok(tmp, &separator);
			
			SSD1289_PrintString(ptr, &p, &droidSans22ptFontInfo);
			p = IncY(&p, 30); p.x = 0;
			drawXSeparator(&p, SSD1289_MaxX, SEPARATOR2);
			p = IncY(&p, 6); p.x = 10;
		
			ptr = strtok(NULL, "\n");
			strcpy(mArtistsDirList[i], ptr);
			counter++;
		}
	}
	if(counter == 0) { // no lines were read == end of file
		mArtistsCurrentPage--;
		Menu_MP3_Artists_Refresh();
	}
}

void Menu_MP3_Songs_Enter() {
	mSongsCurrentPage = 0;
	Menu_MP3_Songs_Refresh();
}

void Menu_MP3_Songs_Refresh() {
	gMenu = MENU_MP3_SONGS;
	SSD1289_ClearOmitBar();
	
	FIL file;
	FRESULT res;
	char tmp[100];
	point p = {10, 28};

	sprintf(tmp, "/music/%s/IDX", mArtistsDirList[mArtistsCurrentDir]);
	res = f_open(&file, tmp, FA_READ | FA_OPEN_EXISTING);
	f_sync(&file);
	CheckRes();
	for(uint8_t i = 0; i < mSongsCurrentPage*ITEMS_ON_PAGE; i++) {
		f_gets(tmp, sizeof(tmp), &file);
		f_sync(&file);
	}

	uint8_t counter = 0;
	for(uint8_t i = 0; i < ITEMS_ON_PAGE; i++) {
		if(f_gets(tmp, sizeof(tmp), &file) == tmp) {
			f_sync(&file);
			char separator = 0x03;
			char *ptr;
			ptr = strtok(tmp, &separator);
			ptr = strtok(NULL, &separator);
			
			SSD1289_PrintString(ptr, &p, &droidSans22ptFontInfo);
			p = IncY(&p, 30); p.x = 0;
			drawXSeparator(&p, SSD1289_MaxX, SEPARATOR2);
			p = IncY(&p, 6); p.x = 10;
			counter++;
		}
	}
	if(counter == 0) { // no lines were read = end of file
		mSongsCurrentPage--;
		Menu_MP3_Songs_Refresh();
	}
}
